package cn.zhqr.lib.bean;

import cn.zhqr.common.util.str.StringUtils;
import cn.zhqr.lib.utils.DateUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态数据库记录
 * Created by charles on 2017/3/30.
 */
public class ActiveMap<K,V> extends LinkedHashMap<K,V> {
    private Logger logger = Logger.getLogger(ActiveMap.class);


    public static ActiveMap create(Object... params){
        ActiveMap record = new ActiveMap();
        for(int i=0;i+1<params.length;i+=2){
            record.put(params[i], params[i + 1]);
        }
        return record;
    }

    public static ActiveMap merge(Map... maps){
        ActiveMap new_map = new ActiveMap<>();
        for(Map map : maps){
            if(map != null){
                new_map.putAll(map);
            }
        }
        return new_map;
    }

    public String getStr(Object key){
        Object value = get(key);
        return value!=null?String.valueOf(get(key)):"";
    }

    public String getStr(Object key,String defaultValue){
        Object value = get(key);
        return value!=null?String.valueOf(value):defaultValue;
    }
    public V get(Object key,Object default_value){
        Object value = get(key);
        return (V)(value!=null?value:default_value);
    }

    public <T> T getValue(Object key,Object defaultValue){
        Object value = get(key);
        return (T)(value!=null?value:defaultValue);
    }

    public Integer getInt(Object key){
        return getInt(key,null);
    }

    public Integer getInt(Object key,Integer defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Integer){
            return (Integer)value;
        }
        try{
            return Integer.valueOf(getStr(key));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }

    public Long getLong(Object key){
        return getLong(key,null);
    }

    public Long getLong(Object key,Long defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Long){
            return (Long)value;
        }
        try{
            return Long.valueOf(getStr(key));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }
    public Double getDouble(Object key){
        return getDouble(key,null);
    }
    public Double getDouble(Object key,Double defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Double){
            return (Double)value;
        }
        try{
            return Double.valueOf(getStr(key));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }

    public Float getFloat(Object key){
        return getFloat(key,null);
    }

    public Float getFloat(Object key,Float defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Float){
            return (Float)value;
        }
        try{
            return Float.valueOf(getStr(key));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }

    public Boolean getBoolean(Object key){
        return getBoolean(key,null);
    }

    public Boolean getBoolean(Object key,Boolean defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof Boolean){
            return (Boolean)value;
        }
        try{
            return Boolean.valueOf(getStr(key));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }

    public BigDecimal getBigDecimal(Object key){
        return getBigDecimal(key,null);
    }

    public BigDecimal getBigDecimal(Object key,BigDecimal defaultValue){
        Object value = get(key);
        if(value == null){
            return defaultValue;
        }
        if(value instanceof BigDecimal){
            return (BigDecimal)value;
        }
        try{
            return new BigDecimal(String.valueOf(value));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return defaultValue;
    }

    public Date getDate(Object key){
        return (Date)get(key);
    }

    public Date getDate(Object key,String dateFormat){
        try{
            String value = getStr(key);
            if(StringUtils.isNotBlank(value)){
                return DateUtils.getDateFormat(dateFormat).parse(value);
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return null;
    }

    public List getList(Object key){
        return (List)get(key);
    }

    public Map getMap(Object key){
        return (Map)get(key);
    }

    public JSONObject getJSONObject(Object key){
        return (JSONObject)get(key);
    }
}
